<?php
include_once("../../Functions/fileSystem.php");
if (empty($_POST) && $_POST) {
    echo "<script>
    alert('上传的文件超过了php.ini中post_max_size选项限制的值！');window.location.href = document.referrer;
    </script>";
} else {
    include_once("../../Functions/connectDatabase.php");
    get_connection();
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if ($_POST["category"] == 0) {
            echo "<script>
            alert('暂无新闻类别！不能添加新闻！');window.location.href = document.referrer;
            </script>";
        }
        if (empty($_POST["title"])) {
            echo "<script>
            alert('新闻标题不能为空！');window.location.href='./Add_V.php'
            </script>";
        } else {
            $title = $_POST["title"];
            $category = $_POST["category"];
            $content = $_POST["content"];
            $user = "admin";       //测试用，后续根据session写入发布用户
            $clicked = 0;
            $time = date("Y-m-d H:i:s");
            $file_name = $_FILES["attachment"]["name"];
            $message = upload($_FILES["attachment"], "../../Public/Uploads");
            if ($message == "文件上传成功！" || $message == "没有选择上传附件！") {
                $sql = "INSERT INTO news values
            (null,'$user','$category','$title','$content','$time',$clicked,'$file_name')";
                if (mysqli_query($conn, $sql)) {
                    echo "<script>alert('添加成功！');window.location.href='./adminShow.php'</script>";
                } else {
                    $error = mysqli_error($conn);
                    $error = str_replace('\'', '', $error);
                    echo "<script>alert('添加失败！$error');window.location.href='./adminShow.php'</script>";
                }
            } else {
                echo "<script>alert('添加失败！$message');window.location.href='./adminShow.php'</script>";
            }
            $message = urlencode($message);
            //header("Location:List.php?message=$message");
        }
    }
    close_connection();
}
?>